package com.rayboot.footpoint.view;

import java.util.ArrayList;
import java.util.Vector;

import org.taptwo.android.widget.TitleProvider;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.rayboot.footpoint.R;
import com.rayboot.footpoint.activity.FootpointDetailViewActivity;
import com.rayboot.footpoint.activity.NewFootpointActivity;
import com.rayboot.footpoint.db.FootpointManager;
import com.rayboot.footpoint.model.Category;
import com.rayboot.footpoint.model.Footprint;
import com.rayboot.footpoint.utils.FootpointConstant;

public class AsyncAdapter extends BaseAdapter implements TitleProvider {

	private LayoutInflater mInflater;
	private Vector<Object> mData = new Vector<Object>();
	private ArrayList<Footprint> mData1 = null;

	private Context context;

	private class ViewHolder {
		GridView gridView;
	}

	public AsyncAdapter(Context context) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		Cursor cursor = FootpointManager.getInstance().getAllCategory();

		if (cursor != null) {
			cursor.moveToFirst();

			while (false == cursor.isAfterLast()) {

				Category category = new Category(cursor);

				mData.add(category);
				cursor.moveToNext();
			}
			cursor.close();
		}
		this.context = context;
	}

	@Override
	public ArrayList<Footprint> getItem(int position) {
		return mData1;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return drawView(position, convertView);
	}

	private View drawView(int position, View view) {
		ViewHolder holder = null;

		if (view == null) {
			view = mInflater.inflate(R.layout.grid_view, null);

			holder = new ViewHolder();

			holder.gridView = (GridView) view.findViewById(R.id.gvView);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		final ArrayList<Footprint> items = getItem(position);
		if (items != null) {
			holder.gridView.setAdapter(new GridAdapter(context, items));
			holder.gridView.setOnItemClickListener(gridItemClickListener);
		} else {
			new LoadContentTask().execute(position, view);
		}

		return view;
	}

	private OnItemClickListener gridItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			String string = mData1.get(arg2).mPicFileName;
			Intent intent = new Intent(context,
					FootpointDetailViewActivity.class);
			intent.putExtra(FootpointConstant.EXTRA_FOOTPOINT, mData1.get(arg2));
			context.startActivity(intent);
			// Toast.makeText(context, string, Toast.LENGTH_LONG).show();
		}
	};

	@Override
	public String getTitle(int position) {
		return ((Category) mData.get(position)).mName;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	private class LoadContentTask extends AsyncTask<Object, Object, Object> {

		private Integer position;
		private View view;

		@Override
		protected Object doInBackground(Object... arg) {
			position = (Integer) arg[0];
			view = (View) arg[1];

			ArrayList<Footprint> footList = FootpointManager.getInstance()
					.getArrayFootprintsByCategory(
							((Category) mData.get(position)).mKeyword);

			return footList;
		}

		@Override
		protected void onPostExecute(Object result) {
			// process result
			mData1 = (ArrayList<Footprint>) result;

			drawView(position, view);

			view.postInvalidate();
		}

	}

}
